package info.maxhis.bn.sms;

import info.maxhis.bn.R;
import info.maxhis.service.SMSService;
import info.maxhis.util.SharedData;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class SmsActivity extends Activity {
	private EditText smsView;
	private Button saveButton;
	private Context context;
	private SMSService ss;
	private SharedData share;
	private Resources rs;
	private String sid;
	private boolean bNew;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		rs = getResources();
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_sms);
//		setTitle(rs.getString(R.string.title_sms));
		smsView = (EditText) findViewById(R.id.text_sms_body);
		saveButton = (Button) findViewById(R.id.btn_save_sms);
		context = this;
		ss = new SMSService(context);
		share = new SharedData(context);
		sid = share.getSMSId();
		bNew = getIntent().getBooleanExtra("new", false);
		if (sid != null && !bNew) {
			smsView.setText(ss.getSMSById(sid));
		}
		
		//保存短信
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String sms = smsView.getText().toString().trim();
				if (sms == null || sms.equals("")) {
					smsView.setError(rs.getString(R.string.sms_null));
					smsView.requestFocus();
					return ;
				}
				if (sid != null && !bNew) {
					//修改
					ss.updateSMS(sid, sms);
				} else {
					//新增
					ss.addSMS(sms);
				}
				Intent intent = new Intent(context, SMSListActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
}
